<template>
  <!--新增对话框-->
  <el-dialog title="修改密码" :visible.sync="dialogVisible" width="450px" :before-close="handleClose">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
      <el-form-item label="原密码" prop="pass" label-width="100px">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpass" label-width="100px">
        <el-input type="password" v-model="ruleForm.newpass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="checkPass" label-width="100px">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label-width="125px">
        <el-button type="primary" @click="submitForm('ruleForm')" style="margin-right:15px">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>


<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入原密码'));
      } else {
        // if (value !== this.password) {
        //   callback(new Error('原密码错误!'));
        // }
        callback();
      }
    };
    var validatePass1 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'));
      } else if (value !== this.ruleForm.newpass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: '',
        newpass: '',
        checkPass: '',
      },
      rules: {
        pass: [
          { validator: validatePass, required: true, trigger: 'blur' }
        ],
        newpass: [
          { validator: validatePass1, required: true, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, required: true, trigger: 'blur' }
        ],
      }
    };
  },
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    // password: {
    //   type: Number,
    //   required: true
    // },
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.put("/adminUpdatePassword/" + this.ruleForm.pass + "/" + this.ruleForm.newpass).then(res => {
            this.handleClose();
            localStorage.clear()
            sessionStorage.clear()
            // 调用vuex的resetState方法，恢复vuex的默认状态
            this.$store.commit("resetState")
            this.$router.push("/login")
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClose() {
      this.$emit("closeDialog");
      this.resetForm('ruleForm')
    },
  }
}
</script>

<style>
</style>
